package com.hs.ding.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.hs.ding.pojo.Transport;
import com.hs.ding.pojo.Vehicle;
import com.hs.ding.service.ITransportService;
import com.hs.ding.service.IVehicleService;
import com.hs.ding.utils.NumberGeneratorUtils;
import com.hs.dto.Constants;
import com.hs.dto.ResultBean;
import com.hs.li.pojo.CarManagement;
import com.hs.li.service.ICarManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YingFeng
 * @since 2022-06-16
 */
@CrossOrigin
@RestController
@RequestMapping("/vehicle")
public class VehicleController {

    @Autowired
    private IVehicleService iVehicleService;
    @Autowired
    private ITransportService transportService;

    @Autowired
    private ICarManagementService carManagementService;

    @PostMapping("/create")
    public ResultBean ceateNew(@RequestBody Vehicle vehicle){

        System.out.println("vehicle = " + vehicle);


        String id = vehicle.getvOrderId();
        UpdateWrapper<Vehicle> uw = new UpdateWrapper<>();
        uw.set("v_status",1);
        uw.eq("v_order_id",id);
        boolean b = iVehicleService.update(uw);

        Transport transport = new Transport();
        transport.setDriverName(vehicle.getDriverName());
        transport.setCarPlate(vehicle.getCarPlate());
        transport.setCarrierName(vehicle.getCarrierName());
        transport.settOrderId(NumberGeneratorUtils.randomNumber());
        transport.setvOrderId(vehicle.getvOrderId());
        boolean b1 = transportService.save(transport);

        carManagementService.changeCarTypeByCarplate(vehicle.getCarPlate());

        return b && b1 ? ResultBean.OK():ResultBean.ERROR();


    }



    @GetMapping("page")
    public ResultBean findAllByPage(Integer currentPage,
                                    Integer pageSize,
                                    String inputOrder,
                                    String inputName,
                                    String inputDeparture){

        ResultBean allVehicle = iVehicleService.findAllVehicle(currentPage, pageSize,inputOrder,inputName,inputDeparture);

        return allVehicle;
    }

    /**
     * 根据条件查询指定对象，并分页
     * 如果条件为空，查询所有
     * @param currentPage 当前页数
     * @param pageSize 总页数
     * @param vOrderId 需求单号
     * @param cName 客户名称
     * @param route1 出发地
     * @param route2 目的的
     * @return
     */
    @GetMapping("/search")
    public ResultBean findByCondition(Integer currentPage,
                                      Integer pageSize,
                                      String inputOrder,
                                      String inputName,
                                      String inputDeparture){
        currentPage = --currentPage;
        if (!StringUtils.hasText(inputOrder)) {
            inputOrder = "";
        }
        if (!StringUtils.hasText(inputName)) {
            inputName = "";
        }
        if (!StringUtils.hasText(inputDeparture)) {
            inputDeparture = "";
        }


        List<Vehicle> vehicles = iVehicleService.searchByCondition(currentPage,pageSize,inputOrder,inputName,inputDeparture);


        ResultBean resultBean = new ResultBean();
        resultBean.setData(vehicles);
        resultBean.setCode(Constants.SUCCESS_CODE);
        resultBean.setMsg("查询成功！");

        return resultBean;
    }

}
